import 'package:flutter/material.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';

class DkSwitchFormField extends DkFormField<bool> {
  DkSwitchFormField({
    Key key,
    @required String attribute,
    String title,
    bool initialValue = true,
    String trueText,
    String falseText,
    bool hasLine = true,
    ValueChanged<bool> onChanged,
    DkFormFieldValidator<bool> validator,
    ValueTransformer valueTransformer,
  }) : super(
          attribute: attribute,
          initialValue: initialValue,
          validator: validator,
          valueTransformer: valueTransformer,
          builder: (DkFormFieldState<bool> field) {
            final DkFormFieldState state = field;
            void onChangedHandler(bool value) {
              if (onChanged != null) {
                onChanged(value);
              }
              field.didChange(value);
            }

            return SwitchChoiceItemWidget(
              title: title,
              rightText: trueText ?? '是',
              leftText: falseText ?? '否',
              open: state.value,
              tapCallback: onChangedHandler,
              isLine: hasLine,
            );
          },
        );
}
